package com.aspire.moss.portal.desktop;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Observable;
import java.util.Set;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;

import com.aspire.moss.portal.R;
import com.aspire.moss.portal.desktop.AppView.OnDeleteClickListener;

public class WorkspaceAdapter extends Observable {

    private static final String TAG = "WorkspaceAdapter";

    private Context mContext;

    private Map<Integer, List<AppInfo>> apps;

    private int addiconPageId, addiconPos_no = 0;

    private OnClickListener onClickListener;

    private OnDeleteClickListener onDeleteClickListener;

    private OnLongClickListener onLongClickListener;

//    private OnAppViewChangeListener onAppViewChangeListener;

    public WorkspaceAdapter(Context mContext, Map<Integer, List<AppInfo>> apps) {
        this.mContext = mContext;
        this.apps = apps;
    }

    public Map<Integer, List<AppInfo>> getApps() {
        return apps;
    }

    public void setApps(Map<Integer, List<AppInfo>> apps) {
        this.apps = apps;
    }

    public View getGroupView(ViewGroup parment, int index) {
        return Utils.getLayoutInflater(mContext).inflate(R.layout.celllayou, null);
    }

    public AppView getChildView(int group, int index) {
        Log.d(TAG, "getChildView group=" + group + " index=" + index);
        final AppInfo info = apps.get(group).get(index);
        final AppView view = newChildView();
        view.setAppInfo(info);
        view.setText(info.app_name);
        // if (null != info.iconBitmap) {
        // ((ImageView) view.findViewById(R.id.icon))
        // .setImageBitmap(info.iconBitmap);
        // view.setIcon(info.iconBitmap , false);
        // }
        // final TextView textView = ((TextView) view.findViewById(R.id.title));
        return view;
    }

    public AppView newChildView() {
        Log.d(TAG, "newChildView");
        final AppView view = (AppView) Utils.getLayoutInflater(mContext).inflate(R.layout.app, null);
        if (null != onClickListener)
            view.setOnClickListener(onClickListener);
        if (null != onLongClickListener)
            view.setOnLongClickListener(onLongClickListener);
        if (null != onDeleteClickListener) {
            view.setOnDeleteClickListener(onDeleteClickListener);
        }
//        if (null != onAppViewChangeListener) {
//            view.setOnAppViewChangeListener(onAppViewChangeListener);
//        }
        return view;
    }
    /**
     * 得到有多少屏幕
     * 
     * @return
     */
    public int getCount() {
        return apps.size();
    }

    /**
     * 添加 一个屏幕
     * 
     * @param screenId
     */
    public void addGroupView(int screenId) {
        if (null != apps) {
            apps.put(screenId, new ArrayList<AppInfo>());
        }
    }

    public void addChildView(AppInfo appInfo, int group) {
        this.apps.get(group).add(appInfo);
//        AppDataBO.getInstance().addHomeApp(appInfo.app_id, appInfo.page_no, appInfo.pos_no);
    }

    /**
     * 添加 添加应用图标
     * 
    public void addAddIconView() {
        if (null != apps) { // 找到最大页
            for (final Iterator<Integer> it = apps.keySet().iterator(); it.hasNext();) {
                final int index = it.next();
                if (addiconPageId < index) {
                    addiconPageId = index;
                }
            }
            // 找到最大
            if (null != apps.get(addiconPageId) && apps.get(addiconPageId).size() < 16) {
                List<AppInfo> infos = apps.get(addiconPageId);
                int max = 0;
                for(AppInfo info : infos){
                    if(max < info.pos_no){
                        max = info.pos_no;
                    }
                }
                
                //添加
                final AppInfo addAppInfo = new AppInfo(); 
                addAppInfo.app_id = null;
                addAppInfo.icon_file_name = "icon/icon_add.png";
                addAppInfo.default_app = 2;
                addAppInfo.page_no = addiconPageId;
                addAppInfo.pos_no = max + 1;
                addAppInfo.app_name = "添加应用";
                addiconPos_no = max;
                this.addChildView(addAppInfo, addiconPageId);
            }
        }
    }*/

    /**
     * 删除 添加应用图标
     * 
     *
    public void removeAddIconView() {
        this.apps.get(addiconPageId).remove(addiconPos_no);
    }
*/
    public List<AppInfo> getGroup(int page_no) {
        final List<AppInfo> group = apps.get(page_no);
        return group != null ? group : null;
    }

    /**
     * 通知
     */
    public void notifyObservers() {
        setChanged();
        notifyObservers(null);
    }

    /**
     * 删除一个应用的数据
     * 
     * @param appInfo
     */
    public void removeAppInfo(AppInfo appInfo) {
        if (null != appInfo) {
            final int page_no = appInfo.page_no;
            final List<AppInfo> group = getGroup(page_no);
            if (null != group) {
                for (final AppInfo info : group) {
                    if (info.eqPosScreen(appInfo) && group.remove(info)) {// 删除在数据源里的数据
//                        AppDataBO.getInstance().removeHomeApp(info.app_id);
                        return;
                    }
                }
            }
        }
    }

    public void printInfo() {
        if (null != apps) {
            final Set<Entry<Integer, List<AppInfo>>> sets = apps.entrySet();
            List<AppInfo> aps;
            Integer integer = null;
            for (Entry<Integer, List<AppInfo>> entry : sets) {
                integer = entry.getKey();
                if (null != integer) {
                    aps = apps.get(integer);
                    for (AppInfo info : aps) {
                        Log.i(TAG, "info:" + info);
                    }
                }
            }
        }

    }

    public OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public OnDeleteClickListener getOnDeleteClickListener() {
        return onDeleteClickListener;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }

    public OnLongClickListener getOnLongClickListener() {
        return onLongClickListener;
    }

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

//    public OnAppViewChangeListener getOnAppViewChangeListener() {
//        return onAppViewChangeListener;
//    }
//
//    public void setOnAppViewChangeListener(OnAppViewChangeListener onAppViewChangeListener) {
//        this.onAppViewChangeListener = onAppViewChangeListener;
//    }

}
